package com.woniuxy.sale.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.sale.ChooseProduceMsg;
import com.woniuxy.commons.entity.sale.Product;
import com.woniuxy.commons.entity.sale.Product1;
import com.woniuxy.commons.entity.sale.SoAccount;
import com.woniuxy.commons.entity.sale.dto.ChooseProduceMsgDto;
import com.woniuxy.commons.entity.sale.dto.ProductDto;
import com.woniuxy.commons.enums.DictCodeEnum;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.sale.config.DictConfig;
import com.woniuxy.sale.mapper.ChooseProduceMsgDao;
import com.woniuxy.sale.mapper.ProductDao;
import com.woniuxy.sale.mapper.SoAccountDao;
import com.woniuxy.sale.service.ChooseProduceMsgService;
import com.woniuxy.commons.utils.DictUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: CHL  <br/>
 * @ToDo: <br>
 * Date: 2022/4/19:16:52  <br/>
 * Description:
 */
@Service
public class ChooseProduceMsgServiceImpl implements ChooseProduceMsgService {
    @Resource
    ChooseProduceMsgDao chooseProduceMsgDao;


    @Override
    public List<ChooseProduceMsgDto> findProduceById(int id) {

        return chooseProduceMsgDao.findProduceById(id);
    }

    @Override
    @Cacheable(value = "find",key = "#soId")
    public Result findChooseProduceBySoId(Integer currentPage, Integer limit, Integer soId) {

        PageHelper.startPage(currentPage, limit);

        List<ChooseProduceMsg> chooseProduceMsgs =
                chooseProduceMsgDao.findChooseProduceBySoId(soId);
        PageInfo<ChooseProduceMsg> of = PageInfo.of(chooseProduceMsgs);
        PageUtils pageUtils = new PageUtils(of);

        List<ChooseProduceMsgDto> chooseProduceMsgDtos = new LinkedList<>();
        pageUtils.setList(chooseProduceMsgDtos);

        chooseProduceMsgs.forEach(chooseProduceMsg ->
        {
            ChooseProduceMsgDto chooseProduceMsgDto = new ChooseProduceMsgDto();
            Product product = chooseProduceMsg.getProduct();
            ProductDto productDto = new ProductDto();
            chooseProduceMsgDto.setProductDto(productDto);
            BeanUtils.copyProperties(chooseProduceMsg, chooseProduceMsgDto);


            BeanUtils.copyProperties(product, productDto);

            ProductServiceImpl.setField(product, productDto, "condition", DictCodeEnum.CONDITION);


            ProductServiceImpl.setField(product, productDto, "perGram",
                    Math.toIntExact(DictConfig.getDictOne(DictCodeEnum.WORKMANSHIP.getId(), product.getWorkmanship()).getId()));

            ProductServiceImpl.setField(product, productDto, "region", DictCodeEnum.REGION);
            ProductServiceImpl.setField(product, productDto, "productStatus", DictCodeEnum.PRODUCE_STATUS);

            ProductServiceImpl.setField(product, productDto, "depository",
                    Objects.requireNonNull(DictCodeEnum.getDictCode
                            (Math.toIntExact(DictConfig.getDictOne(DictCodeEnum.REGION.getId(),
                                    product.getRegion()).getId()))));

            ProductServiceImpl.setField(product, productDto, "singleOrNormal", DictCodeEnum.ITEM_TYPE);
            ProductServiceImpl.setField(product, productDto, "workmanship", DictCodeEnum.WORKMANSHIP);
            ProductServiceImpl.setField(product, productDto, "productType", DictCodeEnum.CATEGORY);

            chooseProduceMsgDtos.add(chooseProduceMsgDto);

        });

        return Result.ok(pageUtils);
    }

    @Override
//    @Cacheable(value = "find",key = "#soId")
    public Result findChooseProduceBySoId(Integer soId) {


        List<ChooseProduceMsg> chooseProduceMsgs =
                chooseProduceMsgDao.findChooseProduceBySoId(soId);
        PageInfo<ChooseProduceMsg> of = PageInfo.of(chooseProduceMsgs);


        List<ChooseProduceMsgDto> chooseProduceMsgDtos = new LinkedList<>();


        chooseProduceMsgs.forEach(chooseProduceMsg ->
        {
            ChooseProduceMsgDto chooseProduceMsgDto = new ChooseProduceMsgDto();
            Product product = chooseProduceMsg.getProduct();
            ProductDto productDto = new ProductDto();
            chooseProduceMsgDto.setProductDto(productDto);
            BeanUtils.copyProperties(chooseProduceMsg, chooseProduceMsgDto);


            BeanUtils.copyProperties(product, productDto);

            ProductServiceImpl.setField(product, productDto, "condition", DictCodeEnum.CONDITION);


            ProductServiceImpl.setField(product, productDto, "perGram",
                    Math.toIntExact(DictConfig.getDictOne(DictCodeEnum.WORKMANSHIP.getId(), product.getWorkmanship()).getId()));

            ProductServiceImpl.setField(product, productDto, "region", DictCodeEnum.REGION);
            ProductServiceImpl.setField(product, productDto, "productStatus", DictCodeEnum.PRODUCE_STATUS);

            ProductServiceImpl.setField(product, productDto, "depository",
                    Objects.requireNonNull(DictCodeEnum.getDictCode
                            (Math.toIntExact(DictConfig.getDictOne(DictCodeEnum.REGION.getId(),
                                    product.getRegion()).getId()))));

            ProductServiceImpl.setField(product, productDto, "singleOrNormal", DictCodeEnum.ITEM_TYPE);
            ProductServiceImpl.setField(product, productDto, "workmanship", DictCodeEnum.WORKMANSHIP);
            ProductServiceImpl.setField(product, productDto, "productType", DictCodeEnum.CATEGORY);

            chooseProduceMsgDtos.add(chooseProduceMsgDto);

        });
        return Result.ok(chooseProduceMsgDtos);
    }

    @Resource
    private ProductDao productDao;

    @Resource
    private SoAccountDao soAccountDao;

    @Override
    public Result dictTest() {
        List<Product1> product1s = productDao.queryAll();

//        ArrayList<Product1> objects = new ArrayList<>();
//        product1s.forEach(product1 -> objects.add(DictUtils.setDict(product1)));

      //  List<?> objects1 = DictUtils.setListDict(product1s);

       // return Result.ok(objects1);
        return test();
    }

    public Result test(){
      List<SoAccount> soAccounts =   soAccountDao.findAll();
        List<?> objects1 = DictUtils.setListDict(soAccounts);
        return Result.ok(objects1);
    }





}
